//
//  ClearCache.m
//  news
//
//  Created by imacbookair on 16/3/9.
//  Copyright © 2016年 imacbookair. All rights reserved.
//

#import "ClearCache.h"

#import <SDImageCache.h>



@implementation ClearCache



/// 获取单个文件的大小
+ (float)fileSizeAtPath:(NSString *)path
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:path]) {
        long long size = [fileManager attributesOfItemAtPath:path error:nil].fileSize;
        return size / 1024.0 / 1024.0;
    }
    return 0;
}

/// 记录目录大小
+ (float)folderSizeAtPath:(NSString *)path
{
    // 创建管理器对象
    NSFileManager *fileManager = [NSFileManager defaultManager];
    // 获取沙盒路径
//    NSString *cache = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
    

    CGFloat j;
    if ([fileManager fileExistsAtPath:path]) {
        NSArray *files = [fileManager subpathsAtPath:path];
        // 遍历所有文件
        for (NSInteger i = 0; i < files.count; i++) {
            NSString *filePath = [path stringByAppendingPathComponent:files[i]];
            // 获取文件大小
            float k = [[fileManager attributesOfItemAtPath:filePath error:nil] fileSize];
            j = j + k;
            
        }
    }
    return j / 1024.0 / 1024.0;
}

/// 清除缓存文件
+ (void)clearCache:(NSString *)path
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:path]) {
        NSArray *childerFiles = [fileManager subpathsAtPath:path];
        for (NSString *fileName in childerFiles) {
            // 如果有需要, 加入条件, 过滤掉不想删除的文件.
            
            NSString *absolutePath = [path stringByAppendingPathComponent:fileName];
            [fileManager removeItemAtPath:absolutePath error:nil];
        }
    }
    [[SDImageCache sharedImageCache] cleanDisk];
}

@end
